'use strict';

CartCtrl.$inject = [
    '$scope',
    '$location',
    'merchantService'
    // 'WelcomeService'
];

function CartCtrl($scope, $location, merchantService) {
    var vm=this;
    vm.merchantName = JSON.parse(sessionStorage.merchant).mname;
    

    if(null!=sessionStorage.cartInfo){
          vm.cartInfo = JSON.parse(sessionStorage.cartInfo);
    }else{
        vm.cartInfo = [];
    }
    vm.decrementQuantity = function(foodInfo){
        if(null!=sessionStorage.cartInfo){
            var cartInfo = JSON.parse(sessionStorage.cartInfo);
            for(let i = 0;i<cartInfo.length;i++){
              if(cartInfo[i].food._id == foodInfo.food._id){
                if(cartInfo[i].count > 0){
                    cartInfo[i].count -= 1;
                }
              }
            }
            console.log(cartInfo);
            sessionStorage.cartInfo = JSON.stringify(cartInfo);
            vm.cartInfo = cartInfo;
            // $scope.$apply()
          }else{
            console.log("session过期了");
            $location.path("/login/"+icnum);
          }
    }

    vm.incrementQuantity = function(foodInfo){
        if(null!=sessionStorage.cartInfo){
            var cartInfo = JSON.parse(sessionStorage.cartInfo);
            for(let i = 0;i<cartInfo.length;i++){
                console.log(cartInfo[i].food._id);
                console.log(foodInfo._id);
              if(cartInfo[i].food._id == foodInfo.food._id){
                    cartInfo[i].count += 1;
              }
            }
            console.log(cartInfo);
            sessionStorage.cartInfo = JSON.stringify(cartInfo);
            vm.cartInfo = cartInfo;
            // $location.path("/cart");
            // $scope.$apply()
          }else{
            console.log("session过期了");
            $location.path("/login/"+icnum);
          }
    }

    vm.removeFromCart = function(foodInfo) {
        // swal({
        //     title: "你确定要删除该商品吗？",
        //     text: "是否选择删除该商品",
        //     type: "warning",
        //     showCancelButton: true,
        //     confirmButtonColor: "#00ff00",
        //     confirmButtonText: "删除",
        //     closeOnConfirm: false
        //   },
        //   function(){
              
        //     swal("Deleted!", "删除商品成功！", "success");
             
        //   });

        if(null!=sessionStorage.cartInfo){
            var cartInfo = JSON.parse(sessionStorage.cartInfo);
            for(let i = 0;i<cartInfo.length;i++){
              if(cartInfo[i].food._id == foodInfo.food._id){
                    cartInfo.splice(i,1);
              }
            }
            console.log(cartInfo);
            sessionStorage.cartInfo = JSON.stringify(cartInfo);
            vm.cartInfo = cartInfo;
          }else{
            console.log("session过期了");
            $location.path("/login/"+icnum);
          }   
    };

    vm.cartTotal = function(){
        var total = 0;
        var cartInfo = JSON.parse(sessionStorage.cartInfo);
        for(let i = 0;i<cartInfo.length;i++){
            total += cartInfo[i].count * cartInfo[i].food.fprice;
        }
        sessionStorage.sum = total;
        return total;
    }
    vm.cartCount = function(){
        var count = 0;
        var cartInfo = JSON.parse(sessionStorage.cartInfo);
        for(let i = 0;i<cartInfo.length;i++){
            count += cartInfo[i].count;
        }
        
        return count;
    }

    vm.settlement = function(icnum){
        merchantService.findMerchantByIcnum(icnum,function(res){
            sessionStorage.merchant = JSON.stringify(res.data);
            $location.path("/pay");
        })
    }
    
}

module.exports=CartCtrl;